package cn.ztr.ymcc.web.controller;

import cn.ztr.ymcc.constance.LoginCons;
import cn.ztr.ymcc.constance.cons;
import cn.ztr.ymcc.domain.KillCourse;
import cn.ztr.ymcc.dto.KillDto;
import cn.ztr.ymcc.service.IKillActivityService;
import cn.ztr.ymcc.domain.KillActivity;
import cn.ztr.ymcc.query.KillActivityQuery;
import cn.ztr.ymcc.result.JsonResult;
import cn.ztr.ymcc.result.PageList;
import cn.ztr.ymcc.utils.LoginContextUtils;
import org.joda.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.List;

@RestController
@RequestMapping("/killActivity")
public class KillActivityController {

    @Autowired
    public IKillActivityService killActivityService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LoginContextUtils loginContextUtils;

    /**
    * 保存和修改公用的
    */
    @PostMapping("/save")
    public JsonResult saveOrUpdate(@RequestBody KillActivity killActivity){
        if(killActivity.getId()!=null){
            killActivityService.updateById(killActivity);
        }else{
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd");
            String be = simpleDateFormat.format(killActivity.getBeginTime());
            String en = simpleDateFormat.format(killActivity.getEndTime());
            String tiem = be+"至"+en;
            killActivity.setTimeStr(tiem);
            killActivityService.save(killActivity);
        }
        return JsonResult.me();
    }

    /**
    * 删除对象
    */
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        killActivityService.removeById(id);
        return JsonResult.me();
    }

    /**
   * 获取对象
   */
    @GetMapping("/{id}")
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.me().setData(killActivityService.getById(id));
    }


    /**
    * 查询所有对象
    */
    @GetMapping("/list")
    public JsonResult list(){
        return JsonResult.me().setData(killActivityService.list(null));
    }


    /**
    * 带条件分页查询数据
    */
    @PostMapping("/pagelist")
    public JsonResult page(@RequestBody KillActivityQuery query){
        Page<KillActivity> page = new Page<KillActivity>(query.getPageNo(), query.getPageSize());
        page = killActivityService.page(page);
        return JsonResult.me().setData(new PageList<KillActivity>(page.getTotal(), page.getRecords()));
    }

    /**
     * 从redis查询当前秒杀活动的所有课程
     */
    @GetMapping("/find2Redis/{id}")
    public JsonResult find2Redis(@PathVariable("id")Long id){
        List killCourese = redisTemplate.opsForHash().values(cons.KILL_COURSE+id);
        return JsonResult.me(killCourese);
    }

    /**
     * 根据课程id从redis查询秒杀课程
     */
    @GetMapping("/fin2RedisOne/{id}/{courseId}")
    public JsonResult fin2RedisOne(@PathVariable("id")Long id,@PathVariable("courseId")Long KillCourseId){
        KillCourse o = (KillCourse) redisTemplate.opsForHash().get(cons.KILL_COURSE + id, KillCourseId.toString());
        return JsonResult.me(o);
    }

    /**
     * 用户秒杀课程
     * @param killDto
     * @param request
     * @return
     * @throws Exception
     */
   @PostMapping("/killCourse")
    public JsonResult killCourse(@RequestBody KillDto killDto, HttpServletRequest request) throws Exception {
       String header = request.getHeader(LoginCons.TOKEN);
       //获取用户id
       Long userId = loginContextUtils.getUserId(header);
       killDto.setLoginId(userId);
       return killActivityService.killCourse(killDto);
   }


}
